Option Explicit

' ======================================================================
' Descriptor
' delete的组成部分的解析
' ======================================================================

' ======================================================================
' Implement
' ======================================================================

Implements ISqlFormatterSqlSubEnvAnalyzer

' ======================================================================
' Field
' ======================================================================
' ==================================================
' Field
' ==================================================
Private context As SqlFormatterAnalyzer

' ======================================================================
' Constructor
' ======================================================================

Private Sub Class_Terminate()
    Set context = Nothing
End Sub

' ======================================================================
' Public Function
' ======================================================================
' 初始化
Public Sub Init(ByRef analyzer As SqlFormatterAnalyzer)
    Set context = analyzer
End Sub

' ======================================================================
' implements ISqlFormatterSqlSubEnvAnalyzer Function
' ======================================================================
' 获取当前解析结果的类型
Public Function ISqlFormatterSqlSubEnvAnalyzer_getType() As SqlTokenType
    ISqlFormatterSqlSubEnvAnalyzer_getType = SqlTokenType.KEY_DELETE
End Function

' 解析开始部分的作用域 -- 主要负责各类型的sql起始关键字开始的部分的解析
'
' @param tokenScanner 扫描器
' @return 开始部分的作用域
Public Function ISqlFormatterSqlSubEnvAnalyzer_parseStartCompositionEnv(ByRef tokenScanner As ObjArrayRefScanner) As ISqlFormatterElem
    ' 解析delete作用域的起始部分
    Set ISqlFormatterSqlSubEnvAnalyzer_parseStartCompositionEnv = context.parseDeleteStartEnv(tokenScanner)
End Function

' 解析、生成各个组成部分的作用域
'
' @param tokenScanner 扫描器
' @param leftParenthesisStack 左括号栈 (ObjStack<SqlToken>)
' @param tokenType 起始token的类型
' @return 各组部分的作用域
Public Function ISqlFormatterSqlSubEnvAnalyzer_parseSubCompositionEnv( _
    ByRef tokenScanner As ObjArrayRefScanner, _
    ByRef leftParenthesisStack As ObjStack, _
    ByRef tokenType As SqlTokenType) As ISqlFormatterElem

    With context ' -- 在这里加速
        Dim result As ISqlFormatterElem

        ' 1. 检查: 如果 `tokenType` 是 using，解析一个 using 作用域，并保存到 `result`
        If tokenType = SqlTokenType.KEY_USING Then
            Set ISqlFormatterSqlSubEnvAnalyzer_parseSubCompositionEnv = .parseUsingEnv(tokenScanner)

        ' 2. 检查: 如果 `tokenType` 是 from，解析一个 From 作用域，并保存到 `result`
        ElseIf tokenType = SqlTokenType.KEY_FROM Then
            Set ISqlFormatterSqlSubEnvAnalyzer_parseSubCompositionEnv = .parseFromEnv(tokenScanner)

        ' 3. 检查: 如果 `tokenType` 是 left / right / inner / cross / join ，解析一个 join 作用域，并保存到 `result`
        ElseIf SqlHelper.tokenTypeIsJoinStartSqlKey(tokenType) Then
            Set ISqlFormatterSqlSubEnvAnalyzer_parseSubCompositionEnv = .parseJoinEnv(tokenScanner)

        ' 4. 检查: 如果 `tokenType` 是 where，解析一个 where 作用域，并保存到 `result`
        ElseIf tokenType = SqlTokenType.KEY_WHERE Then
            Set ISqlFormatterSqlSubEnvAnalyzer_parseSubCompositionEnv = .parseConditionEnv(tokenScanner)

        ' 5. 检查: 如果 `tokenType` 是 ON，解析一个 ON 条件作用域，并保存到 `result`
        ElseIf tokenType = SqlTokenType.KEY_ON Then
            Set ISqlFormatterSqlSubEnvAnalyzer_parseSubCompositionEnv = .parseConditionEnv(tokenScanner)

        ' 6. 检查: 如果 `tokenType` 是 ORDER_BY，解析一个 ORDER_BY 作用域，并保存到 `result`
        ElseIf tokenType = SqlTokenType.KEY_ORDER_BY Then
            Set ISqlFormatterSqlSubEnvAnalyzer_parseSubCompositionEnv = .parseOrderByEnv(tokenScanner)

        ' 7. 检查: 如果 `tokenType` 是 LIMIT，解析一个 LIMIT 作用域，并保存到 `result`
        ElseIf tokenType = SqlTokenType.KEY_LIMIT Then
            Set ISqlFormatterSqlSubEnvAnalyzer_parseSubCompositionEnv = .parseLimitEnv(tokenScanner)

        ' 8. 如果是其他类型的 token，则退出处理，并返回 `null`
        Else
            Exit Function
        End If

        ' 9. 括号配对
        ' 9.1. 检查: 如果 `result` 不是空，则继续
        If Not ISqlFormatterSqlSubEnvAnalyzer_parseSubCompositionEnv Is Nothing Then
            ' 9.2. 执行: 作用域括号配对，结果保存到 `result`
            Set ISqlFormatterSqlSubEnvAnalyzer_parseSubCompositionEnv = .matchEnvBoundaryParenthesis( _
                ISqlFormatterSqlSubEnvAnalyzer_parseSubCompositionEnv, tokenScanner, leftParenthesisStack)
        End If

    End With

    ' 10. 结束，返回 `result`
    ' return result
End Function
